package com.common.valid;

import com.common.Util.StringUtils;
import com.common.valid.annotation.Enum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * @program: Member_Perks_Store
 * @description: 枚举校验
 * @author: 无恙
 * @create: 2024-06-17 11:21
 **/

public class EnumValidator implements ConstraintValidator<Enum,Object> {
    private List<String> listValues;

    @Override
    public void initialize(Enum annotation) {
        listValues = Arrays.asList(annotation.values());
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if(StringUtils.isBlank(value.toString())) return true;
        String valueString = String.valueOf(value);
        return listValues.contains(valueString);
    }
}
